package com.yang;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class ContactHelper extends SQLiteOpenHelper {

    public static final String TABLE_NAME = "information";

    private Context context;

    public ContactHelper(@Nullable Context context) {
        super(context, "ContactDB", null, 1);
        this.context = context;
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL("CREATE TABLE information(" +
                "_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
                "name VARCHAR(50), " +
                "phone VARCHAR(255))");
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }

    public Boolean insert(String name, String phone) {
        if (name.trim().length() == 0 || phone.trim().length() == 0) {
            Toast.makeText(context, "请输入完整信息", Toast.LENGTH_LONG).show();
            return false;
        }
        SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("name", name.trim());
        contentValues.put("phone", phone.trim());
        long insertCount = sqLiteDatabase.insert(TABLE_NAME, null, contentValues);
        if (insertCount > 0) {
            Toast.makeText(context, "添加成功", Toast.LENGTH_LONG).show();
            sqLiteDatabase.close();
            return true;
        }
        Toast.makeText(context, "添加失败", Toast.LENGTH_LONG).show();
        sqLiteDatabase.close();
        return false;
    }

    public ArrayList<Map<String, Object>> queryListMap() {
        SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();
        Cursor query = sqLiteDatabase.query(TABLE_NAME,
                null,
                null,
                null,
                null,
                null,
                null);
        query.moveToFirst();
        if (query.getCount() == 0) {
            sqLiteDatabase.close();
            return null;
        } else {
            ArrayList<Map<String, Object>> list = new ArrayList<>();
            while (query.moveToNext()) {
                Map<String, Object> map = new HashMap<>();
                map.put("name", query.getString(1));
                map.put("tel", query.getString(2));
                list.add(map);
            }
            query.close();
            sqLiteDatabase.close();
            return list;
        }
    }

    public String queryAll() {
        SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();
        Cursor query = sqLiteDatabase.query(TABLE_NAME,
                null,
                null,
                null,
                null,
                null,
                null);
        query.moveToFirst();
        if (query.getCount() == 0) {
            sqLiteDatabase.close();
            return null;
        } else {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("ID:" + query.getInt(0)
                    + " 姓名:"
                    + query.getString(1)
                    + " 电话:" + query.getString(2) + "\n");
            while (query.moveToNext()) {
                stringBuilder.append("ID:" + query.getInt(0)
                        + " 姓名:"
                        + query.getString(1)
                        + " 电话:" + query.getString(2) + "\n");
            }
            query.close();
            sqLiteDatabase.close();
            return stringBuilder.toString();
        }
    }

    public String queryPhoneByName(String name) {
        if (name.trim().length() == 0) {
            Toast.makeText(context, "请输入完整信息", Toast.LENGTH_LONG).show();
            return null;
        }
        SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();
        Cursor query = sqLiteDatabase.query(
                TABLE_NAME,
                null,
                "name=?",
                new String[]{name.trim()},
                null,
                null,
                null);
        query.moveToFirst();
        if (query.getCount() == 0) {
            sqLiteDatabase.close();
            return null;
        } else {
            String phone = query.getString(2);
            query.close();
            sqLiteDatabase.close();
            return phone;
        }
    }

    public int deleteByName(String name) {
        if (name.trim().length() == 0) {
            Toast.makeText(context, "请输入完整信息", Toast.LENGTH_LONG).show();
            return -1;
        }
        SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
        int deleteCount = sqLiteDatabase.delete(TABLE_NAME, "name=?", new String[]{name});
        if (deleteCount > 0) {
            Toast.makeText(context, "删除成功", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(context, "删除失败", Toast.LENGTH_LONG).show();
        }
        sqLiteDatabase.close();
        return deleteCount;
    }

    public int deleteById(Integer id) {
        if (id == null ) {
            Toast.makeText(context, "请输入完整信息", Toast.LENGTH_LONG).show();
            return -1;
        }
        SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
        int deleteCount = sqLiteDatabase.delete(TABLE_NAME, "_id=?", new String[]{id.toString()});
        if (deleteCount > 0) {
            Toast.makeText(context, "删除成功", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(context, "删除失败", Toast.LENGTH_LONG).show();
        }
        sqLiteDatabase.close();
        return deleteCount;
    }

    public int update(String name, String phone) {
        if (name.trim().length() == 0 || phone.trim().length() == 0) {
            Toast.makeText(context, "请输入完整信息", Toast.LENGTH_LONG).show();
            return -1;
        }
        SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("phone", phone.trim());
        int updateCount = sqLiteDatabase.update(TABLE_NAME, contentValues, "name=?", new String[]{name});
        if (updateCount > 0) {
            Toast.makeText(context, "更新成功", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(context, "更新失败", Toast.LENGTH_LONG).show();
        }
        return updateCount;
    }
}
